package com.turing.post.common.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.CommonApplicationException;
import com.turing.post.common.util.CommonRespCode;
import com.turing.post.common.util.Global;
import com.turing.post.common.util.MerchantOper;
import com.turing.post.common.util.MerchantOperBusiness;
import com.turing.post.common.util.MerchantOperDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;
import com.turing.post.util.SHA1;

/**
 * 商户操作员业务逻辑实现类
 * 
 * @author 李治
 * 
 */
@Stateless(mappedName = "common.impl.MerchantOperBusinessImpl")
@Remote
@Local
public class MerchantOperBusinessImpl implements MerchantOperBusiness {

	@EJB(mappedName = "common.impl.MerchantOperDaoImpl")
	private MerchantOperDao merchantOperDaoImpl;
	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@Override
	public MerchantOper getMerchantOperByMrchOperId(BigInteger mrchOperId) {
		return merchantOperDaoImpl.getMerchantOperByMerchantOperId(mrchOperId);
	}

	@Override
	public List<MerchantOper> getMerchantOpers(Integer page) {
		return merchantOperDaoImpl.getMerchantOpers(page);
	}

	@Override
	public List<MerchantOper> getMerchantOpersByMerchantNo(String merchantNo,
			Integer page) {
		return merchantOperDaoImpl.getMerchantOpersByMerchantNo(merchantNo,
				page);
	}

	@Override
	public Integer getMerchantOpersCount() {
		return merchantOperDaoImpl.getMerchantOpersCount();
	}

	@Override
	public Integer getMerchantOpersCountByMerchantNo(String merchantNo) {
		return merchantOperDaoImpl
				.getMerchantOpersCountByMerchantNo(merchantNo);
	}

	@Override
	public void insertMerchantOper(MerchantOper merchantOper)
			throws PCPSApplicationException {
		if (null == merchantOper)
			errException("参数merchantOper为空");
		if (PCPSUtil.isEmpty(merchantOper.getMrchOperLogonName()))
			errException("登录名不能为空");
		if (merchantOper.getMrchOperLogonName().trim().length() < 6)
			errException("登录名长度不能小于6位");
		MerchantOper oper = merchantOperDaoImpl
				.getMerchantOperByMrchOperLogonName(merchantOper
						.getMrchOperLogonName());
		if (null != oper)
			errException("该登录名 [ " + merchantOper.getMrchOperLogonName()
					+ " ] 已存在");

		merchantOper.setPwdErrCnt(0);
		StringBuffer news = new StringBuffer(merchantOper
				.getMrchOperLogonName());
		String encryptionPWD = new SHA1().getDigestOfString(news.append(
				merchantOper.getMrchOperPasswd().trim()).toString().getBytes());
		merchantOper.setMrchOperPasswd(encryptionPWD);
		merchantOper.setLastPwdChangeTime(commonDaoImpl.getDatabaseSysTime());
		merchantOperDaoImpl.insertMerchantOper(merchantOper);
	}

	@Override
	public MerchantOper merchantOperLogon(MerchantOper merchantOper)
			throws PCPSApplicationException {
		if (null == merchantOper) {
			errException("商户操作员登录参数为空");
		}
		if (null == merchantOper.getMrchOperLogonName()) {
			errException("商户操作员登录名为空");
		}
		if (null == merchantOper.getMerchantNo()) {
			errException("商户号为空");
		}
		MerchantOper oper = merchantOperDaoImpl
				.getMerchantOperByMrchOperLogonName(merchantOper
						.getMrchOperLogonName());
		if (null == oper)
			errException("登录用户不存在");
		if (!oper.getMerchantNo().equals(merchantOper.getMerchantNo())) {
			errException("商户操作员与所在商户的商户号不匹配");
		}
		if ("0".equals(oper.getStatus()))
			errException("用户已停用，禁止登录");
		Global global = commonDaoImpl.getGlobalInfo();
		if (global.getMaxPwdErrCnt() > 0) {
			if (oper.getPwdErrCnt() >= global.getMaxPwdErrCnt()) {
				// 判断是否在global登录限制时间之内，限制时间分钟
				if ((commonDaoImpl.getDatabaseSysTime().getTime() - oper
						.getLastPwdErrTime().getTime()) / 60000 < global
						.getPwdErrLimit()) {
					errException("基础数据Global表数据有误，系统管理员必须先维护参数数据!");
				}
				errException("密码输入错误次数超过" + global.getMaxPwdErrCnt()
						+ "次，系统限制登录");
			}
		}
		// 判断密码
		StringBuffer logonName = new StringBuffer(merchantOper
				.getMrchOperLogonName());
		String encryptionPWD = new SHA1().getDigestOfString(logonName.append(
				merchantOper.getMrchOperPasswd().trim()).toString().getBytes());
		if (!oper.getMrchOperPasswd().equals(encryptionPWD)) {
			oper.setPwdErrCnt(oper.getPwdErrCnt() + 1);
			oper.setLastPwdErrTime(commonDaoImpl.getDatabaseSysTime());
			merchantOperDaoImpl.updateMerchantOper(oper);
			errException("密码输入错误!");
			return null;
		} else {
			if (oper.getPwdErrCnt().compareTo(0) > 0) {
				oper.setPwdErrCnt(0);
				merchantOperDaoImpl.updateMerchantOper(oper);
			}
			MerchantOper returnOper = getMerchantOperByMrchOperId(oper
					.getMrchOperId());
			return returnOper;
		}
	}

	@Override
	public void changeMerchantOperPwd(MerchantOper merchantOper,
			String newPassWord) throws PCPSApplicationException {
		if (null == merchantOper)
			errException("参数merchantOper为空");
		if (PCPSUtil.isEmpty(newPassWord))
			errException("新密码为空");
		MerchantOper _MerchantOper = merchantOperDaoImpl
				.getMerchantOperByMerchantOperId(merchantOper.getMrchOperId());
		if (null == _MerchantOper)
			errException("商户操作员不存在");
		if (PCPSUtil.isEmpty(merchantOper.getMrchOperLogonName()))
			errException("商户操作员登录名不能为空");
		if (PCPSUtil.isEmpty(merchantOper.getMrchOperPasswd())) {
			errException("原始密码不能为空");
		}
		// 用户输入密码
		String oldPwd = new SHA1().getDigestOfString((merchantOper
				.getMrchOperLogonName() + merchantOper.getMrchOperPasswd())
				.getBytes());
		String newPwd = new SHA1().getDigestOfString((merchantOper
				.getMrchOperLogonName() + newPassWord).getBytes());
		if (!oldPwd.equals(_MerchantOper.getMrchOperPasswd()))
			errException("原始密码输入错误");
		_MerchantOper.setMrchOperPasswd(newPwd);
		_MerchantOper.setPwdErrCnt(0);
		_MerchantOper.setLastPwdChangeTime(commonDaoImpl.getDatabaseSysTime());
		merchantOperDaoImpl.updateMerchantOper(_MerchantOper);
	}

	@Override
	public MerchantOper getMerchantOperQuestionByMrchOperLogonName(
			String merchantOperLogonName) {
		return merchantOperDaoImpl
				.getMerchantOperQuestionByMrchOperLogonName(merchantOperLogonName);
	}

	@Override
	public void getLossPassword(MerchantOper merchantOper)
			throws PCPSApplicationException {
		if (null == merchantOper)
			errException("参数merchantOper为空");
		MerchantOper _MerchantOper = merchantOperDaoImpl
				.getMerchantOperByMrchOperLogonName(merchantOper
						.getMrchOperLogonName());
		if (null == _MerchantOper)
			errException("商户操作员不存在");
		boolean isanswer = false;
		// 判断安全问题答案是否正确
		if (!PCPSUtil.isEmpty(merchantOper.getAnswer1())) {
			isanswer = true;
		}
		if (!PCPSUtil.isEmpty(merchantOper.getAnswer2())) {
			isanswer = true;
		}
		if (!PCPSUtil.isEmpty(merchantOper.getAnswer3())) {
			isanswer = true;
		}
		if (!isanswer) {
			errException("所有安全问题答案都为空!");
		}
		// 有一个答案正确时找回密码
		if (_MerchantOper.getAnswer1().equals(merchantOper.getAnswer1())
				|| _MerchantOper.getAnswer2().equals(merchantOper.getAnswer2())
				|| _MerchantOper.getAnswer3().equals(merchantOper.getAnswer3())) {
			StringBuffer news = new StringBuffer(merchantOper
					.getMrchOperLogonName());
			String newPwd = new SHA1().getDigestOfString(news.append(
					merchantOper.getMrchOperPasswd().trim()).toString()
					.getBytes());
			_MerchantOper.setMrchOperPasswd(newPwd);
			_MerchantOper.setPwdErrCnt(0);
			_MerchantOper.setLastPwdChangeTime(commonDaoImpl
					.getDatabaseSysTime());
			// 修改
			merchantOperDaoImpl.updateMerchantOper(_MerchantOper);
		}
	}

	@Override
	public void updateMerchantOper(MerchantOper merchantOper)
			throws PCPSApplicationException {
		if (null == merchantOper)
			errException("参数merchantOper为空");
		MerchantOper _MerchantOper = merchantOperDaoImpl
				.getMerchantOperByMerchantOperId(merchantOper.getMrchOperId());
		if (null == _MerchantOper)
			errException("商户操作员不存在");
		_MerchantOper.setMrchOperName(merchantOper.getMrchOperName());
		_MerchantOper.setSex(merchantOper.getSex());
		if (!PCPSUtil.isEmpty(merchantOper.getStatus())) {
			_MerchantOper.setStatus(merchantOper.getStatus());
		}
		_MerchantOper.setLinkTele(merchantOper.getLinkTele());
		_MerchantOper.setEmail(merchantOper.getEmail());
		_MerchantOper.setQuestion1(merchantOper.getQuestion1());
		_MerchantOper.setAnswer1(merchantOper.getAnswer1());
		_MerchantOper.setQuestion2(merchantOper.getQuestion2());
		_MerchantOper.setAnswer2(merchantOper.getAnswer2());
		_MerchantOper.setQuestion3(merchantOper.getQuestion3());
		_MerchantOper.setAnswer3(merchantOper.getAnswer3());
		_MerchantOper.setRemark(merchantOper.getRemark());
		merchantOperDaoImpl.updateMerchantOper(_MerchantOper);
	}

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new CommonApplicationException(
				CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
				CommonRespCode.USERLOGON, error);
	}

	@Override
	public void deleteMerchantOper(BigInteger merchantOperId)
			throws PCPSApplicationException {
		merchantOperDaoImpl.deleteMerchantOper(merchantOperId);
	}

	/**
	 * 商户操作员重置密码
	 * 
	 * @param 商户操作员对象
	 * @param 正确返回新密码
	 * @throws PCPSApplicationException
	 */
	public String resetMerchantOperPassword(PCPSWebInput webInput,
			MerchantOper merchantOper) throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(merchantOper.getMrchOperLogonName())) {
			throw new CommonApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					CommonRespCode.EDIT_SYSOPUSER, "输入操作员为空!");
		}
		MerchantOper _merchantOper = merchantOperDaoImpl
				.getMerchantOperByMrchOperLogonName(merchantOper
						.getMrchOperLogonName().trim());
		if (_merchantOper == null) {
			// 用户不存在
			throw new CommonApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					CommonRespCode.EDIT_SYSOPUSER, "操作员不存在!");
		}
		String password = PCPSUtil.getRandom(6);
		String newPassword = new SHA1().getDigestOfString((_merchantOper
				.getMrchOperLogonName() + password).getBytes());
		_merchantOper.setMrchOperPasswd(newPassword);
		_merchantOper.setPwdErrCnt(0);
		_merchantOper.setLastPwdChangeTime(commonDaoImpl.getDatabaseSysTime());
		merchantOperDaoImpl.updateMerchantOper(_merchantOper);
		return password;
	}

}
